<?php
/*. require_module 'standard'; .*/
/**
 * Helper methods
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @link       http://my-trac.assembla.com/amemorytree/ticket/163
 */
class Helper
{
    /**
     * Store whether magic quotes are enabled
     *
     * @var bool
     */
    private $_isMagicQuotes;
    /**
     * Tasks to perform when instantiated
     *
     * @return void
     */
    public function __construct()
    {
        $magicQuotes = get_magic_quotes_gpc();
        $this->_isMagicQuotes = !empty($magicQuotes);
    }
    /**
     * Escape a value
     *
     * @param string $value The text to be escaped
     * @return string      The text after it has been made safe for display
     */
    private function _stripSlashes($value)
    {
        if ($this->_isMagicQuotes) {
            $value = stripslashes($value);
        }
        return $value;
    }
    /**
     * Escape values by removing slashes
     *
     * @param string &$text    The text to be escaped
     * @return string       The text after it has been made safe for display
     */
    public function stripSlashes(&$text)
    {
        if (gettype($text) === 'string') {
            $text = $text;
            return $this->_stripSlashes($text);
        }
        return '';
    }
    /**
     * Escape values by removing slashes
     *
     * @param array[string]mixed &$arr  The array to be escaped
     * @return array[string]mixed       The array after it has been made safe for display
     */
    public function stripAllSlashes(&$arr)
    {
        foreach ($arr as $key => $value) {
            if (gettype($value) === 'string') {
                $str = (string) $value;
                $arr[$key] = $this->stripSlashes($str);
            }
        }
        return $arr;
    }
    /**
     * Safely retrieve a $_GET value
     *
     * @return string
     * @param string $name
     */
    public function get($name) {
        $text = '';
        if (!empty($_GET[$name])) {
            $text = (string) $_GET[$name];
        }
        return $text;
    }
    /**
     * Safely retrieve a $_GET value
     *
     * @return string
     * @param string $name
     */
    public function post($name) {
        $text = '';
        if (!empty($_POST[$name])) {
            $text = (string) $_POST[$name];
        }
        return $text;
    }
    /**
     * Convert a value to a pretty money string
     *
     * @return string
     * @param float $price
     */
    public function money($price) {
        return '$'.number_format($price, 2);
    }
    /**
     * A comma separated list, with "and" before the last term.
     *
     * @return string
     * @param array[int]string $terms
     */
    function commaSeparate($terms)
    {
        $commaSeparated = '';
        if (!empty($terms)) {
            $commaSeparated .= (string)array_pop($terms);
        }
        if (!empty($terms)) {
            $commaSeparated = (string)array_pop($terms).', and '.$commaSeparated;
        }
        if (!empty($terms)) {
            $remainder = implode(', ', $terms);
            $commaSeparated = $remainder.', '.$commaSeparated;
        }
        return $commaSeparated;
    }
    /**
     * Make adjustments to a provided time
     *
     * @return int
     * @param int $time
     * @param int $yearChange
     * @param int $monthChange
     * @param int $dayChange
     * @param int $hourChange
     * @param int $minuteChange
     * @param int $secondChange
     */
    function changeTime($time, $yearChange=0, $monthChange=0, $dayChange=0, $hourChange=0, $minuteChange=0, $secondChange=0)
    {
        $years = intval(date('y', $time)) + $yearChange;
        $months = intval(date('m', $time)) + $monthChange;
        $days = intval(date('d', $time)) + $dayChange;
        $hours = intval(date('s', $time)) + $hourChange;
        $minutes = intval(date('i', $time)) + $minuteChange;
        $seconds = intval(date('H', $time)) + $secondChange;
        return mktime($seconds, $minutes, $hours, $months, $days, $years);
    }
    /**
     * Import a file and pass its contents to a string
     *
     * @return string
     * @param string $file
     */
    function import($file)
    {
        $content = @file_get_contents($file);
        if ($content === FALSE) {
            $content = '';
        }
        return $content;
    }
}
?>
